﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace DusonFramework.Core.Web
{
    public class Upload
    {
        /// <summary>
        /// 上传文件(小图片，可以出入路径和生成缩略图)
        /// </summary>
        /// <param name="pf">HttpPostedFile</param>
        /// <param name="makeSmall">是否生成缩略图</param>
        /// <param name="path">路径</param>
        /// <param name="filename">输出文件名</param>
        /// <returns>错误信息</returns>
        public static string UploadImage(HttpPostedFile pf, bool makeSmall,string savepath, out string filename)
        {
            string fileExt = GetFileExt(pf.FileName);
            if (!AllowFileExt(fileExt))
            {
                filename = string.Empty;
                return "扩展名不被允许";
            }
            filename = GetNewFileName() + "." + fileExt;
            string path = HttpContext.Current.Server.MapPath("~" + "/" + savepath + "/" + filename);
            pf.SaveAs(path);
            if (makeSmall)
            {
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(path);
                if (bitmap.Width <= 200)
                {
                    bitmap.Save(HttpContext.Current.Server.MapPath("~" + "/" + savepath + "/s_" + filename));
                    pf.InputStream.Dispose();
                    bitmap.Dispose();
                    return string.Empty;
                }
                byte[] data = new byte[pf.ContentLength];
                pf.InputStream.Read(data, 0, pf.ContentLength);
                byte[] data1;
                int w = 200;
                int h = bitmap.Height * (w / bitmap.Width);
                ImageHelper.CreateThumbnail(data, out data1, w, h);
                System.Drawing.Image img = ImageHelper.ByteToImage(data1);
                img.Save(HttpContext.Current.Server.MapPath("~" + "/" + savepath + "/s_" + filename));
                img.Dispose();
                bitmap.Dispose();
                pf.InputStream.Dispose();
            }
            return string.Empty;
        }
        /// <summary>
        /// 上传文件 
        /// </summary>
        /// <param name="pf">HttpPostedFile</param>
        /// <param name="filename">输出文件名</param>
        /// <returns>错误信息</returns>
        public static string UploadFile(HttpPostedFile pf,string path, out string filename)
        {
            return UploadImage(pf, false, path, out filename);
        }

        /// <summary>
        /// 新文件名
        /// </summary>
        /// <returns></returns>
        public static string GetNewFileName()
        {
            int length = 6;
            string fileNameSubffix = string.Empty;

            System.Random random = new Random();
            for (int i = 0; i < length; i++)
            {
                fileNameSubffix += random.Next(10).ToString();
            }

            return DateTime.Now.ToString("MMddhhmmss") + fileNameSubffix;
        }

        /// <summary>
        /// 取得文件扩展名
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <returns></returns>
        static public string GetFileExt(string filename)
        {
            int pos = filename.LastIndexOf(".");
            return filename.Substring(pos + 1);
        }
        /// <summary>
        /// 取得主文件名
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        static public string GetFileMainName(string filename)
        {
            int pos = filename.LastIndexOfAny(new char[] { '\\', '/' });
            int pos1 = 0;
            int len = pos - pos1;
            return filename.Substring(pos1 + 1, len);
        }
        /// <summary>
        /// 允许的文件类型
        /// </summary>
        /// <param name="ext">文件类型</param>
        /// <returns></returns>
        static public bool AllowFileExt(string ext)
        {
            try
            {
                string ex = "jpg,gif,png,bmp,jpeg";
                foreach (string s in ex.Split(','))
                {
                    if (ext.ToLower() == s.ToLower())
                    {
                        return true;
                    }
                }
            }
            catch { return true; }
            return false;
        }
    }
}
